package com.tanglon.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson.JSON;
import com.tanglon.domain.ResponseResult;
import com.tanglon.domain.dto.AddCategoryDTO;
import com.tanglon.domain.entity.Category;
import com.tanglon.domain.vo.CategoryVo;
import com.tanglon.domain.vo.CategoryVo2;
import com.tanglon.domain.vo.ExcelCategoryVo;
import com.tanglon.enums.AppHttpCodeEnum;
import com.tanglon.service.CategoryService;
import com.tanglon.service.impl.ExcelWriteServiceImpl;
import com.tanglon.utils.BeanCopyUtils;
import com.tanglon.utils.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/content/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @Resource
    private ExcelWriteServiceImpl excelWriteService;
    
    @GetMapping("/listAllCategory")
    public ResponseResult listAllCategory(){
        List<CategoryVo> list = categoryService.listAllCategory();
        return ResponseResult.okResult(list);
    }

    @PreAuthorize("@ps.hasPermission('content:category:export')")
    @GetMapping("/export")
    public void export(HttpServletResponse response){
//        try {
//            //设置下载文件的请求头
//            WebUtils.setDownLoadHeader("分类.xlsx",response);
//            //获取需要导出的数据
//            List<Category> categoryVos = categoryService.list();
//
//            List<ExcelCategoryVo> excelCategoryVos = BeanCopyUtils.copyBeanList(categoryVos, ExcelCategoryVo.class);
//            //把数据写入到Excel中
//            EasyExcel.write(response.getOutputStream(), ExcelCategoryVo.class).autoCloseStream(Boolean.FALSE).sheet("分类导出")
//                    .doWrite(excelCategoryVos);
//
//        } catch (Exception e) {
//            //如果出现异常也要响应json
//            ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
//            WebUtils.renderString(response, JSON.toJSONString(result));
//        }

        excelWriteService.exportData(response);

    }

    @GetMapping("/list")
    public ResponseResult list(Integer pageNum, Integer pageSize,String name,String status){
        return categoryService.getList(pageNum,pageSize,name,status);
    }

    @PostMapping
    public ResponseResult addCategory(@RequestBody AddCategoryDTO addCategoryDTO){
        return categoryService.addCategory(addCategoryDTO);
    }

    @GetMapping("/{id}")
    public ResponseResult getCategoryById(@PathVariable("id") Long id){
        return categoryService.getCategoryById(id);
    }

    @PutMapping
    public ResponseResult updateCategory(@RequestBody CategoryVo2 categoryVo2){
        return categoryService.updateCategory(categoryVo2);
    }

    @DeleteMapping("/{id}")
    public ResponseResult deleteCategory(@PathVariable("id") Long id){
        categoryService.removeById(id);
        return ResponseResult.okResult();
    }
}